package com.aidong.carnival.controllers;


import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aidong.carnival.controllers.concerns.Failure;
import com.aidong.carnival.controllers.concerns.Result;
import com.aidong.carnival.controllers.concerns.Success;
import com.aidong.carnival.controllers.concerns.View;
import com.aidong.carnival.entity.Message;
import com.aidong.carnival.service.AllCampaignService;
import com.fasterxml.jackson.annotation.JsonView;

@RestController
@RequestMapping("/allCampaign")
public class AllCampaignController {

    @Autowired
    AllCampaignService allCampaignService;

    /**
     * 通用活动
     */
    @JsonView(View.Base.class)
    @RequestMapping(value = "/addAllBuy", method = RequestMethod.POST)
    //@ResponseBody
    public Result addAllBuy(@RequestParam(value = "jsonData", required = true) String jsonData) {
        JSONObject jsonObj = new JSONObject(jsonData);
        int goodsType = jsonObj.getInt("goodsType");
        int ruleId = jsonObj.getInt("ruleId");
        int quantity = jsonObj.getInt("quantity");
        int idongId = jsonObj.getInt("idongId");
        String contactName = jsonObj.getString("contactName");
        String contactPhone = jsonObj.getString("contactPhone");
        String buyDate = jsonObj.getString("buyDate");
        int addressId = jsonObj.getInt("addressId");
        int pickUpAway = jsonObj.getInt("pickUpAway");

        if (goodsType == 1) {
            //活动
            try {
                Message ms = allCampaignService.addCampaignBuy(ruleId, addressId, idongId, contactName, contactPhone);
                if (ms.getCode() == 0) {
                    return new Failure(ms.getMessage());
                } else {
                    return new Success(ms.getMap());
                }
            } catch (UnsupportedOperationException e) {
                e.printStackTrace();
                return new Failure("当前预约人数较多，请稍后重试!");
            } catch (Exception e) {
                e.printStackTrace();

                return new Failure("系统异常");
            }
        } else if (goodsType == 2) {
            //课程
            try {
                Message ms = allCampaignService.addScheduleBuy(ruleId, idongId, contactPhone, contactName);
                if (ms.getCode() == 0) {
                    return new Failure(ms.getMessage());
                } else {
                    return new Success(ms.getMap());
                }
            } catch (UnsupportedOperationException e) {
                e.printStackTrace();
                return new Failure("当前预约人数较多，请稍后重试!");
            } catch (Exception e) {
                e.printStackTrace();

                return new Failure("系统异常");
            }
        } else if (goodsType == 3 || goodsType == 4 || goodsType == 5) {
            //商品
            if (StringUtils.isBlank(buyDate)) {
                return new Failure("商品无购买日期");
            }
            if (pickUpAway == 0) {
                return new Failure("商品取货方式不对");
            }
            try {
                Message ms = allCampaignService.addFoodBuy(ruleId, goodsType, quantity, addressId, buyDate, idongId, contactName, contactPhone, pickUpAway);
                if (ms.getCode() == 0) {
                    return new Failure(ms.getMessage());
                } else {
                    return new Success(ms.getMap());
                }
            } catch (Exception e) {
                e.printStackTrace();
                return new Failure("系统异常");
            }
        }
        return new Failure("未知的商品类型");
    }
}
